/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.ReferenceProxy;
	import vn.demand.dragon.view.components.ReferenceList;
    
    /**
     * A Mediator for interacting with the EmployeeList component
     */
    public class ReferenceListMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "ReferenceListMediator";
        
        /**
         * Constructor. 
         */
        public function ReferenceListMediator( viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );
			
			referenceProxy = ReferenceProxy( facade.retrieveProxy( ReferenceProxy.NAME ) );
			masterdataProxy = MasterdataProxy( facade.retrieveProxy( MasterdataProxy.NAME ) );
			
			projectList.addEventListener(ReferenceList.SELECT_REFERENCE, onSelectReference);
			projectList.addEventListener(ReferenceList.REFRESH_REFERENCE, onRefreshReference);
			projectList.addEventListener(ReferenceList.NEW_PROJECT, onNewReference);
			projectList.addEventListener(ReferenceList.DELETE_REFERENCE, onDeleteReference);
			projectList.addEventListener(ReferenceList.CLOSE_REFERENCE_LIST, closeReferenceList);
			projectList.addEventListener('openTasks', onOpenTasks);
			projectList.addEventListener('openTaskLogs', onOpenTaskLogs);
			
			projectList.dgQuotation.columns[3].labelFunction = lfProjectStatus;
        }
        
		public function onSelectReference(event:Event):void {
			if (projectList.dgQuotation.selectedIndex < 0) {
				alert.error("You should select a reference");
				return;
			}
			sendNotification(FFIAApplicationFacade.REFERENCE_SELECTED, projectList.dgQuotation.selectedItem);
		}
		
		public function onOpenTasks(event:Event):void {
			sendNotification(CommonCommand.OPEN_PROJECT_TASKS_PANEL, projectList.dgQuotation.selectedItem);
		}
		
		public function onOpenTaskLogs(event:Event):void {
			sendNotification(CommonCommand.OPEN_PROJECT_TASK_LOGS_PANEL, projectList.dgQuotation.selectedItem);
		}

		public function onRefreshReference(event:Event):void {
			sendNotification(FFIAApplicationFacade.REFRESH_REFERENCE, projectList.dgQuotation.selectedItem);
		}
		
		public function closeReferenceList(event:Event):void {
			sendNotification(FFIAApplicationFacade.CLOSE_REFERENCES_LIST, projectList);
		}
		
		public function onNewReference(event:Event):void {
			sendNotification(FFIAApplicationFacade.NEW_REFERENCE);
		}
	
		
		public function onDeleteReference(event:Event):void {
//			var sel: = new Supplier(supplierList.dgSupplier.selectedItem);
			alert.confirm("Do you want to remove?", function(e:CloseEvent) {
    		if (e.detail == Alert.NO) {
        			return;
        		}
        		sendNotification(FFIAApplicationFacade.CLOSE_FINISH_PROJECT_PANEL, projectList.dgQuotation.selectedItem);
				sendNotification(FFIAApplicationFacade.DELETE_REFERENCE, projectList.dgQuotation.selectedItem);
        	});
		}
		
        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ FFIAApplicationFacade.LOAD_REFERENCE_SUCCESS,            
					FFIAApplicationFacade.LOAD_REFERENCE_FAILED,
					FFIAApplicationFacade.BROWSE_REFERENCE_LIST,
					FFIAApplicationFacade.LOAD_MASTERDATA_SUCCESS,
					FFIAApplicationFacade.REFRESH_REFERENCE,
					FFIAApplicationFacade.PROJECT_UPDATED,
					CommonCommand.DELETE_REFERENCE_SUCESSFUL,
					CommonCommand.DELETE_REFERENCE_FAILED
			];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
                case FFIAApplicationFacade.LOAD_REFERENCE_SUCCESS:
                    projectList.dgQuotation.dataProvider = referenceProxy.projectsListDP;
                    projectList.dgQuotation.errorString = null;
                    break;
				case FFIAApplicationFacade.LOAD_REFERENCE_FAILED:
					projectList.dgQuotation.errorString = referenceProxy.errorStatus;
					break;
				case FFIAApplicationFacade.BROWSE_REFERENCE_LIST:
					break;
				case FFIAApplicationFacade.REFRESH_REFERENCE:
					referenceProxy.loadReferences();
					break;
				case FFIAApplicationFacade.LOAD_MASTERDATA_SUCCESS:
					referenceProxy.loadReferences();
					break;
				case FFIAApplicationFacade.PROJECT_UPDATED:
					referenceProxy.loadReferences();
					break;
				case CommonCommand.DELETE_REFERENCE_SUCESSFUL:
					alert.info("Delete reference sucessfull");
					referenceProxy.loadReferences();
					break;
				case CommonCommand.DELETE_REFERENCE_FAILED:
					alert.error("Delete reference failed:\n" + String(note.getBody()));
					break;	
            }
        }
                    
        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return EmployeeList the viewComponent cast to EmployeeList
         */
        protected function get projectList():ReferenceList
		{
            return viewComponent as ReferenceList;
        }
		
//		private function logout( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.APP_LOGOUT );
//		}
//		
//		private function addEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.ADD_EMPLOYEE );
//		}
//		
//		private function updateEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.UPDATE_EMPLOYEE, projectList.employees_li.selectedItem);
//		}
		
        public function lfProjectStatus(item:Object, column:DataGridColumn):String {
        	return masterdataProxy.getProjectStatusById(item[column.dataField]);
        }
		
		private var referenceProxy:ReferenceProxy;
		private var masterdataProxy:MasterdataProxy;
    }
}